package com.ruoyi.vehicle.service.impl;


import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.vehicle.domain.VehiclePositionVo;
import com.ruoyi.vehicle.service.VehicleLocationService;

import lombok.extern.slf4j.Slf4j;

import org.influxdb.InfluxDB;
import org.influxdb.dto.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResult;
import org.springframework.data.geo.GeoResults;

import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


/**
 * 车辆位置服务实现类
 */
@Service
@Slf4j
public class VehicleLocationServiceImpl implements VehicleLocationService {



    /**
     * Redis GEO存储键
     */
    private static final String REDIS_GEO_KEY = "vehicle:positions";
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public AjaxResult saveLocation(VehiclePositionVo position) {
        return null;
    }

    @Override
    public List<VehiclePositionVo> queryNearbyVehicles(Double longitude, Double latitude, Double radius) {
        // 构造地理查询范围：以传入的经纬度为中心，指定半径的圆形区域
        org.springframework.data.geo.Circle within =
                new org.springframework.data.geo.Circle(
                        // 创建经纬度点对象，参数为经度和纬度
                        new org.springframework.data.geo.Point(longitude, latitude),
                        // 创建距离对象，指定半径和度量单位（这里用NEUTRAL表示基础单位）
                        new org.springframework.data.geo.Distance(radius, org.springframework.data.geo.Metrics.NEUTRAL));
// 调用Redis GEO的radius方法，根据指定的键和地理范围查询车辆位置信息
        GeoResults<RedisGeoCommands.GeoLocation<String>> locations =
                stringRedisTemplate.opsForGeo()
                        .radius(REDIS_GEO_KEY, within);
// 用于存储查询到的车辆位置信息的列表
        List<VehiclePositionVo> VehiclePosition = new ArrayList<>();
        // 遍历查询结果中的每个地理结果
        if (locations != null && locations.getContent() != null) {

            for (org.springframework.data.geo.GeoResult<RedisGeoCommands.GeoLocation<String>> result : locations.getContent()) {
                // 获取地理位置信息对象
                RedisGeoCommands.GeoLocation<String> geoLocation = result.getContent();
                // 创建车辆位置信息的VO对象
                VehiclePositionVo vehicleLocation = new VehiclePositionVo();
                vehicleLocation.setCarId(geoLocation.getName()); // 假设车辆ID是字符串
                // 检查地理点是否不为空
                if (geoLocation.getPoint() != null) {
                    // 设置经度
                    vehicleLocation.setLongitude(geoLocation.getPoint().getX());
                    // 设置纬度
                    vehicleLocation.setLatitude(geoLocation.getPoint().getY());
                }
                // 将车辆位置信息添加到列表中
                VehiclePosition.add(vehicleLocation);
            }
        }
        // 返回查询到的车辆位置信息列表
        return VehiclePosition;
    }


}